package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jrtc.base.config.constants.Constants;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.util.RedisUtil;
import com.jrtc.dao.DoctorDao;
import com.jrtc.dao.PublicVideoDao;
import com.jrtc.dao.UrlClassificationDao;
import com.jrtc.service.PublicVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 运动处方视频(publicVideo)表服务实现类
 *
 * @author makejava
 * @since 2022-09-06 15:03:12
 */
@Service("publicVideoService")
public class PublicVideoServiceImpl implements PublicVideoService {
    @Resource
    private PublicVideoDao publicVideoDao;
    @Resource
    private UrlClassificationDao urlClassificationDao;
    @Resource
    private DoctorDao doctorDao;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据id查看运动处方视频信息详情
     * @param prescriptionVideoId
     * @return
     */
    @Override
    public PublicVideo queryPublicVideoById(Long prescriptionVideoId) {
        return publicVideoDao.queryPublicVideoById(prescriptionVideoId);
    }

    /**
     * 运动处方视频列表
     * */
    @Override
    public List<UrlClassification> urlList(String name) {
        List<UrlClassification> urlClassificationList = urlClassificationDao.urlList();
        if (urlClassificationList != null && urlClassificationList.size() > 0){
            for (UrlClassification urlClassification : urlClassificationList) {
                List<PublicVideo> publicVideoList = publicVideoDao.urlList(urlClassification.getId(), name);
                if (publicVideoList != null && publicVideoList.size() > 0){
                    urlClassification.setPrescriptionvideoList(publicVideoList);
                }
            }
        }
        return urlClassificationList;
    }

    /**
     * 动作库视频列表
     * @param token
     * @param publicVideoList
     * @return
     */
    @Override
    public IPage<PublicVideo> videoList(String token, PublicVideoList publicVideoList) throws Exception {
        Doctor doctor = new Doctor();
        if (token != null && !token.equals("")){
            Object object = redisUtil.get(Constants.DOCTOR_LOGIN_TOKEN_KEY.getValue() + token);
            if (object != null){
                ObjectMapper objectMapper = new ObjectMapper();
                doctor = objectMapper.convertValue(object, Doctor.class);
            }
            else {
                throw new Exception("登录已过期");
            }
        }
        List<Long> doctorId = new ArrayList<>();
        List<Doctor> doctorList = doctorDao.queryDoctorListByRoleId(1);
        for (Doctor doctors : doctorList) {
            doctorId.add(Long.valueOf(doctors.getDoctorId()));
        }
        Page<PublicVideo> page = new Page<>(publicVideoList.getPageNo(), publicVideoList.getPageSize());
        PublicVideoRequest request = new PublicVideoRequest();
        request.setDoctorId(doctorId);
        request.setDoctor(doctor.getDoctorId());
        request.setVideoName(publicVideoList.getVideoName());
        request.setType(publicVideoList.getType());
        request.setDoctorRole(doctor.getRoleId());
        IPage<PublicVideo> videoList = publicVideoDao.publicVideoList(page, request.getVideoName(), request.getDoctorRole(), request.getType(), request.getDoctorId(), request.getDoctor());
        return videoList;
    }

    /**
     * 添加运动处方视频信息
     * */
    @Override
    public int insertPrescriptionvideo(String token, PublicVideo publicVideo) throws Exception {
        Doctor doctor = new Doctor();
        if (token != null && !token.equals("")) {
            Object object = redisUtil.get(Constants.DOCTOR_LOGIN_TOKEN_KEY.getValue() + token);
            if (object != null) {
                ObjectMapper objectMapper = new ObjectMapper();
                doctor = objectMapper.convertValue(object, Doctor.class);
            } else {
                throw new Exception("登录已过期");
            }
        } else {
            throw new Exception("请进行登录");
        }
        publicVideo.setDoctorId(doctor.getDoctorId());
        return publicVideoDao.insertPrescriptionvideo(publicVideo);
    }

    /**
     * 修改运动处方视频信息
     * */
    @Override
    public int updatePrescriptionvideo(String token, PublicVideo publicVideo) throws Exception {
        if (token != null && !token.equals("")) {
            Object object = redisUtil.get(Constants.DOCTOR_LOGIN_TOKEN_KEY.getValue() + token);
            if (object == null) {
                throw new Exception("登录已过期");
            }
        } else {
            throw new Exception("请进行登录");
        }
        return publicVideoDao.updatePrescriptionvideo(publicVideo);
    }
}
